<?php
/**
 * Created by PhpStorm.
 * User: 秋刀鱼
 * Date: 2018/8/14
 * Time: 14:19
 */

namespace app\models;

use yii\base\Model;
use Yii;
use \common\helps\tools;//使用工具类

class SpecialManage extends Model
{
    /**
     * 表名
     * @var string
     */
    protected $table = 'd_special_manage';

    /**
     * title 添加专家管理
     * @param $uid
     * @param $create_time
     * @return int|string
     * @throws \yii\db\Exception
     */
    public function addSpecial($uid,$create_time)
    {
        $id = tools::createUuid();
        $field = ['id', 'create_time','user_id'];
        $batchData = [[$id, $create_time,$uid]];
        $add = Yii::$app->db->createCommand()->batchInsert(tools::databaseName().'.'.$this->table, $field, $batchData)->execute();
        if ($add) {
            return $id;
        } else {
            return 1;
        }
    }


    /**
     * title 专家管理列表
     * @param $uid
     * @return array
     */
    public function specialList($uid)
    {
        $query = new \yii\db\Query();

        $list = $query
            ->select(['a.id','a.child_id','a.special_name','a.status','a.create_time','a.user_id','b.item_no','c.project_type','b.project_name'])
            ->from(tools::databaseName(). '.' .$this->table .' AS a')
            ->rightJoin('data_yii.special_project_child AS b', 'b.id = a.child_id')
            ->rightJoin('data_yii.special_project AS c', 'b.project_id = c.id')
            ->where(['a.user_id'=>$uid])
            ->all();
        return $list;
    }


    /**
     * title 删除专家管理
     * @param $id
     * @return int
     * @throws \yii\db\Exception
     */
    public function delSpecial($id)
    {
        $query = new \yii\db\Query();

        //调用$othermodel
        $other = new \app\models\SpecialOther;

        //删除状态表
        $status = $other->selectStatus($id);
        if($status){
            $delStatus = $other->selectStatus($status['id']);
            if(!$delStatus){
                return 2;
            }
        }

        //删除专家管理表
        $del = Yii::$app->db->createCommand()->delete(tools::databaseName().'.'.$this->table, "id = '$id'")->execute();
        if(!$del){
            return 2;
        }

        return $del;
    }


    /**
     * title 专家管理详情
     * @param $id
     * @return array
     */
    public function detailSpecial($id)
    {
        $query = new \yii\db\Query();

        $status = new \app\models\SpecialOther;
        $selectStatus = $status->selectIdStatus($id);
        if (empty($selectStatus)) {
            $data = $query
                ->select(['id','child_id','project_name','status','project_name','create_time','user_id'])
                ->from(tools::databaseName() . '.' . $this->table)
                ->where(['id' => $id])
                ->all();
            $result = [];
            foreach($data as $k => $v){
                $v['item_no'] = '';
                $v['project_type'] = '';
                $v['project_id'] = '';
                $v['user_id'] = '';
                $v['phone'] = '';
                $v['company'] = '';
                $v['job'] = '';
                $result[] = $v;
            }

        } else{

            $result = $query
                ->select(['a.id', 'a.child_id', 'a.project_name', 'a.status', 'a.create_time', 'a.user_id', 'b.item_no', 'c.project_type', 'b.project_id', 'e.phone', 'e.company', 'e.job'])
                ->from(tools::databaseName() . '.' . $this->table . ' AS a')
                ->rightJoin(tools::databaseName() . '.special_project_child AS b', 'a.child_id = b.id')
                ->rightJoin(tools::databaseName() . '.d_special_status AS d', 'd.special_id = a.id')
                ->rightJoin(tools::databaseName() . '.special_project AS c', 'b.project_id = c.id')
                ->rightJoin(tools::databaseName() . '.d_user AS e', 'e.id = d.user_id')
                ->where(['a.id' => $id])
                ->all();
    }

        return $result;
    }



    /**
     * title 保存专家管理
     * @param $id
     * @param $uid
     * @param $child_id
     * @param $special_name
     * @param $status
     * @param $update_time
     * @param $plan_id
     * @return int
     * @throws \yii\db\Exception
     */
    public function saveSpecial($id,$child_id,$special_name,$status,$update_time,$special)
    {

        //调用user  model
        $user = new \app\models\user;
        //调用other model
        $other = new \app\models\specialOther;

        //查询项目进度
        $plan = $other->selectStatus($child_id);
        if(!empty($plan)){
            //用户不为空 删除
            foreach($plan as $k => $v){
                if(!empty($v['user_id'])){
                    //查询用户
                    $findUser = $user->findUser($v['user_id']);

                    if($findUser) {
                        //删除用户
                        $delUser = $user->delUser($v['user_id']);
                        if (!$delUser) {
                            return 2;
                        }

                        //删除此条进度
                        $delStatus = $other->delStatus($v['user_id']);
                        if(!$delStatus){
                            return 2;
                        }
                    }



                }


            }


        }
        if(!empty($special)) {
            foreach ($special as $kk => $vv) {
                $new_id = tools::createUuid();
                //是否已存在用户
                $phoneUser = $user->phoneUser($vv['phone']);
                if(!$phoneUser) {
                    //添加user表
                    $addUser = $user->addUser($new_id, $vv['phone'], $vv['company'], $vv['name'], $vv['job'], $update_time);
                    if (!$addUser) {
                        return 3.1;
                    }
                }

                //添加
                $addStatus = $other->addStauts($vv['plan_id'], $new_id, $child_id, $id);
                if (!$addStatus) {
                    return 3.2;
                }

            }

        }

        //保存专家管理

        $data = ['child_id'=>$child_id,'project_name'=>$special_name,'status'=>$status,'update_time'=>$update_time];
        $save = Yii::$app->db->createCommand()->update(tools::databaseName().'.'.$this->table, $data, "id = '$id'")->execute();
        if($save){
            return 1;
        }else{
            return 3.3;
        }
    }





}